// ==UserScript==
// @name         Micoua_打印机
// @version      1.0.0
// @author       micoua
// @create       2020-10-23 17:40:53
// @update       2021-04-17 20:33:59
// @namespace    https://greasyfork.org/zh-CN/users/162781
// @description  调整打印机管理网页样式

// @include      *

//               ↓ jQuery核心文件 ↓
// @require      http://libs.baidu.com/jquery/2.1.4/jquery.min.js

// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// ==/UserScript==

(function () {
    /** 是否最外 */
    if (self != top) {
        return;
    }

    /** 初始化JQ语法 */
    var $ = $ || window.$;

    /** 定义变量 */
    var curWebURL = window.location.href;
    var giteeRaw = 'https://gitee.com/micoua/public-resources/raw/master';

    /** 基础入口 */
    function main() {
        // 深圳住建局OA
        if (curWebURL.indexOf('http://zjj.sz.gov.cn/fwaq') != -1) {
            // 添加标题图标
            $('head').append('<link rel="icon" sizes="any" mask="" href="' + giteeRaw + '/picture/icon/OAOffice.svg">');
        }

        // 打印机-KONICA MINOLTA C554SeriesPCL
        var ipURL = { '192.168.0.233': 'Print', '192.168.0.250': 'Print2' };
        for (let ip in ipURL) {
            if (curWebURL.indexOf('http://' + ip + '/wcd/') != -1) {
                // 添加标题图标
                $('head').append('<link rel="icon" sizes="any" mask="" href="' + giteeRaw + '/picture/icon/' + ipURL[ip] + '.svg">');
                // 超时跳转到作业页面
                if (curWebURL.indexOf('/wcd/timeout.xml') != -1 || curWebURL.indexOf('/wcd/user.cgi') != -1) {
                    window.location.href = 'http://' + ip + '/wcd/job_active.xml';
                }
            }
        }

        // 业昕打印机-鉴定部（废）
        if (false && curWebURL.indexOf('http://192.168.0.200/web/guest/cn/websys/webArch/mainFrame.cgi') != -1) {
            // 添加标题图标
            $('head').append('<link rel="icon" sizes="any" mask="" href="' + giteeRaw + '/picture/icon/Print.svg">');
            var yxJDprintURL = 'http://192.168.0.200/web/guest/cn';
            // 添加元素
            $('html').append('<div class="micoua" id="control-div"></div>');
            $('#control-div').append('<img class="icon-btn" id="stretch-btn" src="' + giteeRaw + '/picture/icon/RightArrow.svg">');
            $('#control-div').append('<img class="icon-btn" id="all-refresh-btn" src="' + giteeRaw + '/picture/icon/Refresh.svg">');
            $('#control-div').append('<img class="icon-btn" id="auto-refresh-btn" src="' + giteeRaw + '/picture/icon/SwitchClose.svg">');
            $('html').append('<div class="micoua" id="status-windows"><iframe src="' + yxJDprintURL + '/websys/webArch/getStatus.cgi"></iframe></div>');
            $('html').append('<div class="micoua" id="cur-job-windows"><iframe src="' + yxJDprintURL + '/websys/jobList/jobListGetList.cgi"></iframe></div>');
            $('html').append('<div class="micoua" id="job-history-windows"><iframe src="' + yxJDprintURL + '/webprinter/jobHistory.cgi"></iframe></div>');
            // 监听内部页面加载
            listenPageLoad();
            // 监听事件
            listenEvents();

            // 监听内部页面加载
            function listenPageLoad(windowsSelector = '') {
                var selectorList = ['#status-windows', '#cur-job-windows', '#job-history-windows'];
                for (const selector of selectorList) {
                    if (windowsSelector === '' || windowsSelector === selector) {
                        $('div' + selector + ' iframe').load(function () {
                            setIframeStyles(selector);
                            console.log('[' + getCurTime() + ']：配置' + selector + '样式');
                        });
                    }
                }

                // 配置iframe内部元素样式
                function setIframeStyles(windowsSelector) {
                    $('div#status-windows iframe').contents().find('html').css('overflow-x', 'hidden');
                    $('div#job-history-windows iframe').contents().find('body').css('width', '1000px');
                    // 隐藏元素
                    const displays = {
                        '#status-windows': [
                            '#button',
                            '.clear.Bodder',
                            '#frame_btm',
                            '#wrap > div.clear.listarea.pdg-T1e.mgn-T1eB1e > div:nth-child(1)',
                            '#wrap > div.clear.listarea.pdg-T1e.mgn-T1eB1e > ul:nth-child(2)',
                            '#wrap > div.clear.listarea.pdg-T1e.mgn-T1eB1e > div.listheader.mgn-T1e',
                            'body > table:nth-child(2) > tbody > tr:nth-child(1)',
                            'body > table:nth-child(2) > tbody > tr:nth-child(3)',
                            'body > table:nth-child(2) > tbody > tr:nth-child(4)',
                        ],
                        '#cur-job-windows': [
                            'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(1)',
                            'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(2)',
                            'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(6)',
                        ],
                        '#job-history-windows': [
                            'body > table:nth-child(1) > tbody > tr:nth-child(1)',
                            'body > table:nth-child(1) > tbody > tr:nth-child(3)',
                            'body > table:nth-child(1) > tbody > tr:nth-child(4)',
                            'body > table:nth-child(2)',
                            'body > img',
                            'body > table:nth-child(6) > tbody > tr > td:nth-child(2) > table:nth-child(1)',
                            'body > table:nth-child(6) > tbody > tr > td:nth-child(2) > table:nth-child(3)',
                        ],
                    };
                    for (const ele of displays[windowsSelector]) {
                        $('div' + windowsSelector + ' iframe')
                            .contents()
                            .find(ele)
                            .css('display', 'none');
                    }
                    // 设置样式
                    const styles = {
                        '.clear.listarea.pdg-T1e.mgn-T1eB1e': { 'margin-top': '35px', 'padding-top': 0 },
                        '#h4tab, body > table:nth-child(2) > tbody > tr:nth-child(2), body > table:nth-child(1)': {
                            position: 'fixed',
                            'z-index': 1,
                            top: 0,
                            left: 0,
                            background: 'white',
                        },
                        '#h4': { 'margin-left': '15px' },
                        'body > table:nth-child(1)': { width: '650px' },
                    };
                    for (const selector in styles) {
                        for (const key in styles[selector]) {
                            $('div.micoua iframe').contents().find(selector).css(key, styles[selector][key]);
                        }
                    }
                    // 监听窗口刷新
                    listenRefresh(windowsSelector);
                }
            }

            // 监听窗口刷新
            function listenRefresh(windowsSelector) {
                const listenClicks = {
                    '#status-windows': [$('div#status-windows iframe').contents().find('#h4tab > ul > li:nth-child(1)')],
                    '#cur-job-windows': [
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find('body > table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr > td:nth-child(3)'),
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(3) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(1)'
                            ),
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(3) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(3)'
                            ),
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(3) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(7)'
                            ),
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(3) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(9)'
                            ),
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(3) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(13)'
                            ),
                    ],
                    '#job-history-windows': [
                        $('div#job-history-windows iframe')
                            .contents()
                            .find('body > table:nth-child(1) > tbody > tr:nth-child(2) > td:nth-child(2) > table > tbody > tr > td:nth-child(3)'),
                        $('div#job-history-windows iframe')
                            .contents()
                            .find('body > table:nth-child(4) > tbody > tr > td:nth-child(2) > table.standard > tbody > tr > td:nth-child(1)'),
                        $('div#job-history-windows iframe')
                            .contents()
                            .find('body > table:nth-child(4) > tbody > tr > td:nth-child(2) > table.standard > tbody > tr > td:nth-child(3)'),
                        $('div#job-history-windows iframe')
                            .contents()
                            .find('body > table:nth-child(4) > tbody > tr > td:nth-child(2) > table.standard > tbody > tr > td:nth-child(7)'),
                        $('div#job-history-windows iframe')
                            .contents()
                            .find('body > table:nth-child(4) > tbody > tr > td:nth-child(2) > table.standard > tbody > tr > td:nth-child(9)'),
                    ],
                };
                const listenCheckboxs = {
                    '#cur-job-windows': [
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(2) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td > table > tbody > tr > td:nth-child(3) > select'
                            ),
                        $('div#cur-job-windows iframe')
                            .contents()
                            .find(
                                'body > table:nth-child(3) > tbody > tr > td:nth-child(2) > table:nth-child(4) > tbody > tr > td > form > table:nth-child(3) > tbody > tr > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > table > tbody > tr > td:nth-child(1) > table > tbody > tr > td:nth-child(16) > select'
                            ),
                    ],
                    '#job-history-windows': [
                        $('div#job-history-windows iframe')
                            .contents()
                            .find('body > table:nth-child(4) > tbody > tr > td:nth-child(2) > table.standard > tbody > tr > td:nth-child(12) > select'),
                    ],
                };
                if (listenClicks.hasOwnProperty(windowsSelector)) {
                    for (const listen of listenClicks[windowsSelector]) {
                        listen.click(function () {
                            listenPageLoad(windowsSelector);
                            console.log('[' + getCurTime() + ']：监听点击：' + windowsSelector);
                        });
                    }
                }
                if (listenCheckboxs.hasOwnProperty(windowsSelector)) {
                    for (const listen of listenCheckboxs[windowsSelector]) {
                        listen.change(function () {
                            listenPageLoad(windowsSelector);
                            console.log('[' + getCurTime() + ']：监听下拉：' + windowsSelector);
                        });
                    }
                }
            }

            // 监听事件
            function listenEvents() {
                // 伸缩
                $('#stretch-btn').click(function () {
                    if ($('#stretch-btn')[0].src == giteeRaw + '/picture/icon/RightArrow.svg') {
                        $('div.micoua').css({ right: '-670px' });
                        $('#control-div').css({ right: '-50' });
                        $('#stretch-btn')[0].src = giteeRaw + '/picture/icon/LeftArrow.svg';
                    } else {
                        $('div.micoua').css({ right: '5px' });
                        $('#control-div').css({ right: '595px' });
                        $('#stretch-btn')[0].src = giteeRaw + '/picture/icon/RightArrow.svg';
                    }
                });
                // 全部刷新
                $('#all-refresh-btn').click(function () {
                    const refreshSelectors = ['#status-windows iframe', '#cur-job-windows iframe', '#job-history-windows iframe'];
                    for (const selector of refreshSelectors) {
                        $(selector).attr('src', $(selector).attr('src'));
                        console.log('[' + getCurTime() + ']：刷新-' + selector);
                    }
                    listenPageLoad();
                });
                // 自动刷新
                var autoRefreshInterval;
                $('#auto-refresh-btn').click(function () {
                    if ($('#auto-refresh-btn')[0].src == giteeRaw + '/picture/icon/SwitchClose.svg') {
                        $('#auto-refresh-btn')[0].src = giteeRaw + '/picture/icon/SwitchOpen.svg';
                        // 定时
                        autoRefreshInterval = setInterval(function () {
                            const refreshSelectors = ['#status-windows iframe', '#cur-job-windows iframe', '#job-history-windows iframe'];
                            for (const selector of refreshSelectors) {
                                $(selector).attr('src', $(selector).attr('src'));
                                console.log('[' + getCurTime() + ']：刷新-' + selector);
                            }
                            listenPageLoad();
                        }, 10 * 1000);
                    } else {
                        $('#auto-refresh-btn')[0].src = giteeRaw + '/picture/icon/SwitchClose.svg';
                        clearInterval(autoRefreshInterval);
                    }
                });
            }
        }
    }

    // 获取当前时间
    function getCurTime() {
        function getNow(s) {
            return s < 10 ? '0' + s : s;
        }
        const myDate = new Date();
        const year = myDate.getFullYear(); //获取当前年
        const month = myDate.getMonth() + 1; //获取当前月
        const date = myDate.getDate(); //获取当前日
        const h = myDate.getHours(); //获取当前小时数(0-23)
        const m = myDate.getMinutes(); //获取当前分钟数(0-59)
        const s = myDate.getSeconds(); //获取当前秒数数(0-59)
        return year + '-' + getNow(month) + '-' + getNow(date) + ' ' + getNow(h) + ':' + getNow(m) + ':' + getNow(s);
    }

    // 等待
    function sleep(waitSeconds) {
        waitSeconds *= 1000;
        setTimeout(function () { }, waitSeconds);
    }

    /** 加载完所有数据后进入主函数 */
    if (true) main();
})();
